package com.me.crgame.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public abstract class Escena extends Stage {

	public final int VIBRACIOM_CORTA = 30;
	public final int VIBRACIOM_MEDIA = 50;
	public final int VIBRACIOM_LARGA = 70;
	
	protected Image background;
	protected Sound sonidoBotonPulsado;
	protected Music musicaDeFondo;
	

	public Escena() {
		Director d = Director.getInstance();
		super.setViewport(d.getWidth(), d.getHeight(), d.getStreech());
		sonidoBotonPulsado = Gdx.audio.newSound(Gdx.files.internal("data/sistema_menus/boton.mp3"));    
	}

	protected abstract void empiezaLaEscena();

	protected abstract void empiezaLaEscena(int transitionMode);

	protected abstract void terminaLaEscena();

	protected abstract void pause(boolean gamePaused);

	protected abstract void update(float deltaTime);

	protected void render(float deltaTime) {
		super.act(deltaTime);
		super.draw();
	}
	
	protected void drawSprite(Sprite sprite, float alpha){
		super.getSpriteBatch().begin();
		sprite.draw(super.getSpriteBatch(),alpha);
		super.getSpriteBatch().end();
	}
	
	//Array de sprites
	protected void drawSprites(Sprite[] sprite, float alpha){
		super.getSpriteBatch().begin();
		for(Sprite s: sprite){
			s.draw(super.getSpriteBatch(),alpha);
		}
		super.getSpriteBatch().end();
	}

	public void setBackground(String ruta) {
		background = new Image(new Texture(Gdx.files.internal(ruta)));
		background.setFillParent(true);
		super.addActor(background);
	}
	
	public void cambiarEscena(EnumEscena escena, EnumTransicion transicion){
		Director.getInstance().activarEscena(escena, transicion);
	}
	
	public void habilitarRotarEscalar(boolean b){
		if(b){
			super.getRoot().setOriginX(Director.getInstance().getWidth()/2f);
			super.getRoot().setOriginY(Director.getInstance().getHeight()/2f);
		}
		else{
			super.getRoot().setOriginX(0);
			super.getRoot().setOriginY(0);
		}
	}
	
	protected void setScale(float scale){
		this.getRoot().setScale(1f);
	}
	
	public void activarVibracion(int milisecs){
		Gdx.input.vibrate(milisecs);
	}
	
	
	/**
	 * Metodo muy util para saber si la plataforma es o no Android
	 * 
	 * @return boolean que indica si el dispositivo actual es o no Android
	 */
	public boolean isAndroid() {
		return Gdx.app.getType() == ApplicationType.Android;
	}
	
	@Override
	public String toString(){
		return this.getClass().getSimpleName();
	}

}
